package br.com.portalnet.persistence;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;


import br.com.portalnet.model.Equipamento;
import br.com.portalnet.model.Localidade;
import br.com.portalnet.model.Usuario;
import br.com.portalnet.util.HibernateUtil;

public class UsuarioDAO implements IUsuarioDAO{

	@Override
	public void save(Usuario usuario) {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.save(usuario);
			t.commit();
			
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public Usuario getUsuario(long id) {
		Session session = null;
		try
		{
			session = HibernateUtil.getSession().openSession();			
			Usuario user = (Usuario) session.get(Usuario.class, id);
			
			user.getLocalidade().getId();
			user.getNivel().getId();		
			
			return user;
		}finally 
		{
			
			try
			{			
				if(session != null)
				{
						session.close();
				}
			
				
			} catch (Exception e) {
				
			}
		
		}
		
	}

	@Override
	public List<Usuario> list() {
		Session session = null;
				
		try
		{
				
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			//List lista = session.createQuery("from Livro").list();		
			List lista = session.createCriteria(Usuario.class).list();
			t.commit();
			return lista;
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			return null;
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public void remove(Usuario usuario) {
		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.delete(usuario);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
		
	}

	@Override
	public void update(Usuario usuario) {
		Session session = null;
		
		try
		{
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();
			session.update(usuario);
			t.commit();
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();
					
					
				}
			
				
			} catch (Exception e) {
				
			}
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
		
	}

	@Override
	public List<Usuario> findByNomeSenha(String nome, String senha) {
		
		Session session = null;
		
		try
		{		
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();		
			Criteria criteria = session.createCriteria(Usuario.class);
			criteria.add(Restrictions.eq("nome", nome));
			criteria.add(Restrictions.eq("senha", senha));
			List lista = criteria.list();
			t.commit();
			return lista;
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();					
					
				}
			
				
			} catch (Exception e) {
				
			}
			return null;
			
		}
		
		finally 
		{
			session.close();
			
		
		}
		
	}

	@Override
	public Usuario findUsuarioByEmail(String email)
	{
		Session session = null;
		
		try
		{		
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();		
			Criteria criteria = session.createCriteria(Usuario.class);
			criteria.add(Restrictions.eq("email", email));
			Usuario user = (Usuario) criteria.list().get(0);
			t.commit();
			return user;
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();					
					
				}
			
				
			} catch (Exception e) {
				
			}
			return null;
			
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

	@Override
	public Usuario findUsuarioByNome(String nome) {
Session session = null;
		
		try
		{		
			session = HibernateUtil.getSession().openSession();
			Transaction t = session.beginTransaction();		
			Criteria criteria = session.createCriteria(Usuario.class);
			criteria.add(Restrictions.eq("nome", nome));
			Usuario user = (Usuario) criteria.list().get(0);

			

			user.getLocalidade().getId();
			user.getLocalidade().getCidade();
			user.getLocalidade().getEstado();
			user.getNivel().getId();		
			
			
			
			t.commit();
			return user;
		}catch(Exception erro){
			
			try
			{
				
				
				if(session != null)
				{
					if(session.getTransaction().isActive())
						session.getTransaction().rollback();					
					
				}
			
				
			} catch (Exception e) {
				
			}
			return null;
			
		}
		
		finally 
		{
			session.close();
			
		
		}
	}

	
	
}
